const { userMsg, isTimeOut } = require('./requestUserMsg');

module.exports = (app) => {
    return async function validateUser(ctx, next) {
        try {
            let userInfo = userMsg(ctx);
            let isOverTime = await isTimeOut(app, userInfo);
            if (isOverTime) {
                throw ctx.helper.getErrorMsg(203);
            }
            if (!userInfo.hasOwnProperty('limit')) {

                throw ctx.helper.getErrorMsg(201);
            } else if (!userInfo.limit.includes('read')) {

                throw ctx.helper.getErrorMsg(205);
            } else if (!userInfo.limit.includes('download')) {

                throw ctx.helper.getErrorMsg(206);
            } else {
                await next();
            }
        } catch (obj) {
            ctx.response.body = obj;
        }
    }

}